﻿#include <iostream>
#include "custom_data.h"

using namespace boost::interprocess;

int main() {
    struct shm_remove
    {
        shm_remove() { shared_memory_object::remove("my_shared_memory"); }
        ~shm_remove(){ shared_memory_object::remove("my_shared_memory"); }
    } remover;

    managed_shared_memory segment(open_or_create, "my_shared_memory", 524288000);
    custom_data data;
    data.init(segment);

    switch (fork()) {
    case -1:
        std::cout << "fork error" << std::endl;
        exit(-1);

    case 0:
        for (int i = 0; i < 1000000; ++i) {
            data.add();
            data.push_back(i);
        }
        exit(0);

    default:
        break;
    }
    for (int i = 0; i < 1000000; ++i) {
        data.add();
        data.push_back(i);
    }
    sleep(5);
    data.print();

    return 0;
}